package com.zhoujing.myrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 周敬
 * @version 1.0
 * @createTime 2022/7/20-10:45-星期三
 *
 * 配置类
 * 发布确认（高级）
 */
@Configuration
public class ConfirmConfig {

    /**
     * 交换机名称
     */
    public static final String CONFIRM_EXCHANGE = "confirm.exchange";
    /**
     * 备份交换机
     */
    public static final String BACKUP_EXCHANGE = "backup.exchange";
    /**
     * 队列名称
     */
    public static final String CONFIRM_QUEUE = "confirm.queue";
    /**
     * 备份队列
     */
    public static final String BACKUP_QUEUE = "back.queue";
    /**
     * 警告队列
     */
    public static final String WARNING_QUEUE = "warning.queue";
    /**
     * RoutingKey
     */
    public static final String CONFIRM_ROUTING_KEY = "key1";

    /**
     * 声明交换机
     * @return
     */
    @Bean
    public DirectExchange confirmExchange(){
        // 普通交换机设置备份交换机
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE).durable(true).alternate(BACKUP_EXCHANGE).build();
    }

    /**
     * 备份交换机
     * @return
     */
    @Bean
    public FanoutExchange backupExchange(){
        return new FanoutExchange(BACKUP_EXCHANGE);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }

    /**
     * 备份队列
     * @return
     */
    @Bean
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE).build();
    }

    /**
     * 警告队列
     * @return
     */
    @Bean
    public Queue warningQueue(){
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }


    /**
     * 队列绑定交换机
     * @param cExchange 交换机
     * @param cQueue 队列
     * @return
     */
    @Bean
    public Binding cExchangeBinDingCQueue(@Qualifier("confirmExchange") DirectExchange cExchange,@Qualifier("confirmQueue") Queue cQueue){
        return BindingBuilder.bind(cQueue).to(cExchange).with(CONFIRM_ROUTING_KEY);
    }

    /**
     * 备用队里绑定备用交换机
     * @param backupQueue 备用队里
     * @param backupExchange 备用交换机
     * @return
     */
    @Bean
    public Binding backupQueueBinDingBackExchange(@Qualifier("backupQueue") Queue backupQueue,
                                                  @Qualifier("backupExchange") FanoutExchange backupExchange){
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    /**
     * 警告队列绑定备用交换机
     * @param warningQueue 警告队列
     * @param backupExchange 备用交换机
     * @return
     */
    @Bean
    public Binding warningQueueBinDingBackExchange(@Qualifier("warningQueue") Queue warningQueue,
                                                   @Qualifier("backupExchange") FanoutExchange backupExchange){
        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }
}
